package com.lh_travel.lohas.activity.user;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;

import com.lh_travel.lohas.R;
import com.lh_travel.lohas.adapter.CommonAdapter;
import com.lh_travel.lohas.adapter.ViewHolder;
import com.lh_travel.lohas.base.baseActivity;

import java.util.ArrayList;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

public class SharedActivity extends baseActivity {

    private ImageButton img_back;
    private ListView lv_shared;
    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_shared);

        findView();
        bindListner();
        ensureUI();
    }

    @Override
    public void findView() {

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        img_back = (ImageButton) findViewById(R.id.img_back);
        lv_shared = (ListView) findViewById(R.id.lv_shared);
    }

    @Override
    public void bindListner() {

        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void ensureUI() {

        setSupportActionBar(toolbar);
        toolbar.setEnabled(true);

        sharedItem item1 = new sharedItem("分享给我的微信好友", R.drawable.wechat2);
        sharedItem item2 = new sharedItem("分享给我的微信朋友圈", R.drawable.frend2);
        sharedItem item3 = new sharedItem("分享给我的新浪微博", R.drawable.xinlang);

        ArrayList<sharedItem> list = new ArrayList<>();

        list.add(item1);
        list.add(item2);
        list.add(item3);

        CommonAdapter<sharedItem> adapter = new CommonAdapter<sharedItem>(mContext, list, R.layout.item_shared) {
            @Override
            public void convert(ViewHolder helper, sharedItem item, int position) {

                helper.setText(R.id.tv_title, item.title);

                helper.setImageResource(R.id.img_icon, item.icon);

            }
        };

        lv_shared.setAdapter(adapter);

        lv_shared.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String title = "乐活酒店";
                String text = mContext.getString(R.string.share_title);
                Platform.ShareParams sharePlatform = new Platform.ShareParams();
                Platform platform = null;
                String shareUrl = "";

                switch (position) {
                    case 0:
                        platform = ShareSDK.getPlatform(Wechat.NAME);
                        break;

                    case 1:

                        platform = ShareSDK.getPlatform(WechatMoments.NAME);

                        break;

                    case 2:
                        showMessage("分享给我的新浪微博");
                        break;
                }

                sharePlatform.setShareType(Platform.SHARE_WEBPAGE);

                setSharePlatform(sharePlatform, title, text, "");

                if (shareUrl != null && !shareUrl.equalsIgnoreCase("")) {
                    sharePlatform.setUrl(shareUrl);
                }

                platform.share(sharePlatform);

            }
        });

    }

    private void setSharePlatform(Platform.ShareParams sharePlatform, String shareTitle, String shareText, String shareImageUrl) {
        sharePlatform.setTitle(shareTitle);
        if (shareText != null && !shareText.equalsIgnoreCase("")) {
            sharePlatform.setText(shareText);
        }
        if (shareImageUrl != null && !shareImageUrl.equalsIgnoreCase("")) {

            sharePlatform.setImageUrl(shareImageUrl);
            sharePlatform.setImagePath(shareImageUrl);
        }
    }

    public class sharedItem {

        public String title;
        public int icon;

        public sharedItem(String title, int icon) {
            this.title = title;
            this.icon = icon;
        }
    }
}
